package com.itdianzi.service.cargo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.itdianzi.dao.financial.FinancialDao;
import com.itdianzi.dao.invoice.InvoiceDao;
import com.itdianzi.domain.financial.Financial;
import com.itdianzi.domain.financial.FinancialExample;
import com.itdianzi.domain.invoice.Invoice;
import com.itdianzi.service.cargo.FinancialService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class FinancialServiceImpl implements FinancialService {

    @Autowired
    private FinancialDao financialDao;
    @Autowired
    private InvoiceDao invoiceDao;



    @Override
    public Financial findByid(String id) {
        Financial financial = financialDao.selectByPrimaryKey(id);
        return financial;
    }

    @Override
    public void delete(String id) {
        //1.如果删除的财务是 state 0状态为草稿的

        // 就是退回  把发票state 状态 改为0草稿
        //2.如果删除的财务是 state状态为5 财务 已付款 就是 直接删除信息
        Financial financial = findByid(id);
        if (financial.getState()==0) {
            Invoice invoice = new Invoice();
            invoice.setState(0);
            invoiceDao.updateByPrimaryKeySelective(invoice);

        }else{
            financialDao.deleteByPrimaryKey(id);

        }



    }

    @Override
    public void toupdate(Financial financial) {
        financialDao.updateByPrimaryKeySelective(financial);
    }

    @Override
    public void save(Financial financial,String Iid) {

        //1.添加制单日期 已经有了
        //2.添加制单人   已经有了
        //1.state状态 为 0 草稿
        //id 赋予
        //保存所有数据到 财务表
        Invoice invoice = invoiceDao.selectByPrimaryKey(Iid);
       BeanUtils.copyProperties(invoice,financial);
        financial.setState(0);
        financial.setId(Iid);
        financialDao.insertSelective(financial);
    }

    @Override
    public void update(Financial financial) {
        financialDao.updateByPrimaryKeySelective(financial);
    }

    @Override
    public PageInfo findAll(FinancialExample financialExample, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Financial> financials = financialDao.selectByExample(financialExample);
        return new PageInfo(financials);
    }
}
